﻿@model PurchaseOrderLines
@{
    Layout = null;
    int PurchaseOrderLineRow = 3;
}

<div class="form-item l-m-table form l-margin-t20">
    <tablefor for="ProductList">
        <NewTr allow="true" template="template" callback="callback"></NewTr>
        <DeleteTr allow="true"></DeleteTr>
    </tablefor>
    <table id="ProductList"  mark="LedinghuoTable">
        <tr class="bgc3">
            <th>商品编号</th>
            <th>商品名称</th>
            <th>规格</th>
            <th>单位</th>
            <th>批号</th>
            <th>数量</th>
            <th>单价</th>
            <th>金额</th>
            <th>备注</th>
            <th class="t-c">操作</th>
        </tr>
        @if (Model == null || Model.Items.Count <= 0)
        {
            for (int i = 0; i < PurchaseOrderLineRow; i++)
            {
                <tr index="@i">
                    <td><input type="text" class="inp-text full" name="ProductNo_@i" /><acronym for="ProductNo_@i"><req message="商品编号不能为空"></req></acronym></td>
                    <td tag="ProductName_@i" width="200"></td>
                    <td tag="Spec_@i"></td>
                    <td tag="Unit_@i"></td>
                    <td><input type="text" class="inp-text full" name="LotNo_@i"  /><acronym for="LotNo_@i"><req message="批号不能为空"></req></acronym></td>
                    <td width="80"><input type="text" class="inp-text full" name="Quantity_@i"  /><acronym for="Quantity_@i"><req message="数量不能为空"></req></acronym></td>
                    <td width="100"><input type="text" class="inp-text full" name="Price_@i" /><acronym for="Price_@i"><req message="单价不能为空"></req></acronym></td>
                    <td tag="Amount_@i"></td>
                    <td><input type="text" class="inp-text full" name="Remark_@i" /></td>
                    <td>
                        <input type="hidden" name="ProductId_@i"  />
                        <input type="hidden" name="ProductName_@i" />
                        <input type="hidden" name="Spec_@i" />
                        <input type="hidden" name="Unit_@i"  />
                        <input type="hidden" name="Amount_@i"  />
                        <input type="hidden" name="Barcode_@i" />
                        <input type="hidden" name="PurchaseOrderLineId_@i" />
                        <a href="javascript:void(0)" mark="del">删除</a>
                    </td>
                </tr>
            }
        }
        else
        {
            PurchaseOrderLineRow = Model.Items.Count;
            int i=0;
            foreach (PurchaseOrderLine s in Model.Items)
            {
                <tr index="@i">
                    <td><input type="text" class="inp-text full" name="ProductNo_@i"  value="@s.ProductNo" data="@s.ProductId"  /><acronym for="ProductNo_@i"><req message="商品编号不能为空"></req></acronym></td>
                    <td tag="ProductName_@i" width="200">@s.ProductName</td>
                    <td tag="Spec_@i">@s.Spec</td>
                    <td tag="Unit_@i">@s.Unit</td>
                    <td><input type="text" class="inp-text full" name="LotNo_@i"  value="@s.LotNo" /><acronym for="LotNo_@i"><req message="批号不能为空"></req></acronym></td>
                    <td width="80"><input type="text" class="inp-text full" name="Quantity_@i"  value="@s.Quantity.QuantityFormat()" /><acronym for="Quantity_@i"><req message="数量不能为空"></req></acronym></td>
                    <td width="100"><input type="text" class="inp-text full" name="Price_@i"  value="@s.Price.MoneyFormat()" /><acronym for="Price_@i"><req message="单价不能为空"></req></acronym></td>
                    <td tag="Amount_@i">@s.Amount.MoneyFormat()</td>
                    <td><input type="text" class="inp-text full" name="Remark_@i"  value="@s.Remark"/></td>
                    <td>
                        <input type="hidden" name="ProductId_@i"  value="@s.ProductId"/>
                        <input type="hidden" name="ProductName_@i"  value="@s.ProductName"/>
                        <input type="hidden" name="Spec_@i"  value="@s.Spec"/>
                        <input type="hidden" name="Unit_@i"  value="@s.Unit"/>
                        <input type="hidden" name="Amount_@i"  value="@s.Amount"/>
                        <input type="hidden" name="Barcode_@i"  value="@s.Barcode"/>
                        <input type="hidden" name="PurchaseOrderLineId_@i" value="@s.PurchaseOrderLineId" />
                        <a href="javascript:void(0)" mark="del">删除</a>
                    </td>
                </tr>
                i++;
            }
        }
    </table>
    <input type="hidden" name="PurchaseOrderLineRow" id="PurchaseOrderLineRow" value="@PurchaseOrderLineRow" />
</div>
<script type="text/javascript">
    $(function () {
        var mathAmount = function () {
            var amount = 0;
            $("#ProductList").find("tr[index]").each(function (index, ret) {
                var $this = $(ret);
                var a = $this.find("input[name^=Amount_]").val();
                if (a) {
                    amount += parseFloat(a);
                }
            });
            return amount;
        };
        var bindEvent = function (self) {
            $(self).find("input[name^='ProductNo']").autocomplete({
                url: "/Product/GetProductBySearchKey",
                binder: { text: "ProductNo", value: "ProductId" },
                itemEvent: function ($this) {
                    var attributes = ["ProductName", "Spec", "Unit", "LotNo", "Quantity", "Price", "ProductId"];//需要获取的属性值
                    var lowAttributes = ["productname", "spec", "unit", "lotno", "minquantity", "price", "productid"];//对应上边的属性值
                    //暂时没想到别的解决办法：因为从html标签中获取的都是小写
                    var product = {};
                    var $p = $(self);
                    for (var k in $this[0].attributes) {
                        var node = $this[0].attributes[k];
                        var nodeValue = node.nodeValue;
                        var nodeName = node.nodeName;
                        var i = $.inArray(nodeName, lowAttributes);
                        if (i != -1) {
                            $p.find("input[name^=" + attributes[i] + "_]").val(nodeValue).input().doVerification();
                            $p.find("td[tag^=" + attributes[i] + "_]").text(nodeValue);
                        }
                    }
                    //#region 计算金额
                    var quantity = $p.find("input[name^=Quantity_]").val();
                    var price = $p.find("input[name^=Price_]").val();
                    $p.find("td[tag^=Amount_]").text(quantity * price);
                    $p.find("input[name^=Amount_]").val(quantity * price);

                    var amount = mathAmount();
                    //$("input[name=Amount]").val(amount);
                    //$("input[name=FavourableAmount]").val(amount);
                    //$("#Amount").html(amount);
                    //$("#FavourableAmount").html(amount);
                    $("#ActualAmount").val(amount);
                    //#endregion
                }
            });
        }
        $("#ProductList").find("tr").each(function (index) {
            var self = this;
            bindEvent(self);
        });
        window.template=function (index) {
            var html = [];
            html.push('<tr index="'+index+'">');
            html.push('  <td><input type="text" class="inp-text full" name="ProductNo_'+index+'" /><acronym for="ProductNo_'+index+'"><req message="商品编号不能为空"></req></acronym></td>');

            html.push('  <td tag="ProductName_'+index+'" width="200"></td>');
            html.push('  <td tag="Spec_'+index+'"></td>');
            html.push('  <td tag="Unit_'+index+'"></td>');
            html.push('  <td><input type="text" class="inp-text full" name="LotNo_'+index+'"  /><acronym for="LotNo_'+index+'"><req message="批号不能为空"></req></acronym></td>');
            html.push('  <td width="80"><input type="text" class="inp-text full" name="Quantity_'+index+'"  /><acronym for="Quantity_'+index+'"><req message="数量不能为空"></req></acronym></td>');
            html.push('  <td width="100"><input type="text" class="inp-text full" name="Price_'+index+'" /><acronym for="Price_'+index+'"><req message="单价不能为空"></req></acronym></td>');
            html.push('  <td tag="Amount_'+index+'"></td>');
            html.push('  <td><input type="text" class="inp-text full" name="Remark_'+index+'" /></td>');
            html.push('  <td>');
            html.push('      <input type="hidden" name="ProductId_'+index+'"  />');
            html.push('      <input type="hidden" name="ProductName_'+index+'" />');
            html.push('      <input type="hidden" name="Spec_'+index+'" />');
            html.push('      <input type="hidden" name="Unit_'+index+'"  />');
            html.push('      <input type="hidden" name="Amount_'+index+'"  />');
            html.push('      <input type="hidden" name="Barcode_'+index+'" />');
            html.push('      <input type="hidden" name="PurchaseOrderLineId_'+index+'" />');
            html.push('      <a href="javascript:void(0)" mark="del">删除</a>');
            html.push('  </td>');
            html.push('</tr>');
            return html.join("");
        };
        window.callback = function (self) {
            var linerow = $("#PurchaseOrderLineRow").val();
            $("#PurchaseOrderLineRow").val(linerow + 1);
            bindEvent(self);
            $(self).find("input:text").each(function () {
                $(this).input();
            });
        };
    });
</script>